#include "CantorPair.h"

void cantor_prep (int *x, int *y) {
  if (*x > *y) {
    int temp = *x;
    *y = *x;
    *x = temp;
  }

  return;
}

// http://hbfs.wordpress.com/2011/09/27/pairing-functions/
// http://logic.cse.unt.edu/tarau/teaching/SoftEng/pcantor.pdf
uint64_t cantor_pair (uint32_t x, uint32_t y) {
  int64_t p=0;
  int i=0;

  //  std::cout << "H(" << x<< ", " <<y << ")" << std::endl;

  while (x||y) {
      p|= ((uint64_t)(x&1)<<i);     x>>=1;
      p|= ((uint64_t)(y&1)<<(i+1)); y>>=1;
      i+=2;
  }
 
  return p;
}

void cantor_depair (uint64_t p, uint32_t & x, uint32_t & y) {
  x=0;
  y=0;
  int i=0;
  while (p)
    {
      x|=((uint32_t)(p&1)<<i); p>>=1;
      y|=((uint32_t)(p&1)<<i); p>>=1;
      i++;
    }
}
